package com.gz.mylife.widget;


import com.gz.mylife.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GenderSelectButton extends RelativeLayout{
	public static final int DEFAULT = -1;
	public static final int LEFT = 0;
	public static final int RIGHT = 1;
	
	private int mCurrentItem = DEFAULT;
	
	private RelativeLayout mRLLeft;
	private RelativeLayout mRLRight;
	private TextView mTvLeft;
	private TextView mTvRight;
	private SelectButtonListener mListenr;
	
	public void setListener(SelectButtonListener listener){
		this.mListenr = listener;
	}
	
	public int getmCurrentItem() {
		return mCurrentItem;
	}

	public void setmCurrentItem(int mCurrentItem) {
		this.mCurrentItem = mCurrentItem;
		if(mRLLeft == null || mRLRight == null){
			return;
		}
		if(mCurrentItem == LEFT){
			mRLLeft.setSelected(true);
			mRLRight.setSelected(false);
		}else{
			mRLLeft.setSelected(false);
			mRLRight.setSelected(true);
		}
		mTvLeft.setTextColor(getResources().getColor(mCurrentItem == LEFT?R.color.white:R.color.RGB_666666));
		mTvRight.setTextColor(getResources().getColor(mCurrentItem == RIGHT?R.color.white:R.color.RGB_666666));
		
	}

	public GenderSelectButton(Context context) {
		super(context);
	}
	
	public GenderSelectButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onFinishInflate() {
		super.onFinishInflate();
		mRLLeft = (RelativeLayout) findViewById(R.id.rl_select_left);
		mRLRight = (RelativeLayout) findViewById(R.id.rl_select_right);
		mTvLeft = (TextView) findViewById(R.id.tv_select_left);
		mTvRight = (TextView) findViewById(R.id.tv_select_right);
		changeSelected();
		mRLLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mCurrentItem == LEFT){
					return;
				}else{
					mCurrentItem = LEFT;
					changeSelected();
				}
			}
		});
		mRLRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mCurrentItem == RIGHT){
					return;
				}else{
					mCurrentItem = RIGHT;
					changeSelected();
				}
			}
		});
		
		mRLLeft.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if(mCurrentItem == RIGHT){
						mTvLeft.setTextColor(getResources().getColor(R.color.white));
						mRLLeft.setBackgroundResource(R.drawable.drawable_gender_select_left);
					}
//					mTvRight.setTextColor(getResources().getColor(mCurrentItem == RIGHT?R.color.home_blue:R.color.register_littleblue));
					break;
				case MotionEvent.ACTION_MOVE:
					break;
				case MotionEvent.ACTION_UP:
					if(mCurrentItem == RIGHT){
						mTvLeft.setTextColor(getResources().getColor(R.color.RGB_666666));
						mRLLeft.setBackgroundResource(R.drawable.selector_gender_left);
					}
					break;
					
				case MotionEvent.ACTION_CANCEL:
					if(mCurrentItem == RIGHT){
						mTvLeft.setTextColor(getResources().getColor(R.color.RGB_666666));
						mRLLeft.setBackgroundResource(R.drawable.selector_gender_left);
					}
					break;
				case MotionEvent.ACTION_OUTSIDE:
					if(mCurrentItem == RIGHT){
						mTvLeft.setTextColor(getResources().getColor(R.color.RGB_666666));
						mRLLeft.setBackgroundResource(R.drawable.selector_gender_left);
					}
					break;

				default:
					event.getAction();
					break;
				}
				return false;
			}
		});
		mRLRight.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					if(mCurrentItem == LEFT){
						mTvRight.setTextColor(getResources().getColor(R.color.white));
						mRLRight.setBackgroundResource(R.drawable.drawable_gender_select_right);
					}
//					mTvRight.setTextColor(getResources().getColor(mCurrentItem == RIGHT?R.color.home_blue:R.color.register_littleblue));
					break;
				case MotionEvent.ACTION_MOVE:
					
					break;
				case MotionEvent.ACTION_UP:
					if(mCurrentItem == LEFT){
						mTvRight.setTextColor(getResources().getColor(R.color.RGB_666666));
						mRLRight.setBackgroundResource(R.drawable.selector_gender_right);
					}
					break;
				
				case MotionEvent.ACTION_CANCEL:
					if(mCurrentItem == LEFT){
						mTvRight.setTextColor(getResources().getColor(R.color.RGB_666666));
						mRLRight.setBackgroundResource(R.drawable.selector_gender_right);
					}
					break;
				case MotionEvent.ACTION_OUTSIDE:
					if(mCurrentItem == LEFT){
						mTvRight.setTextColor(getResources().getColor(R.color.RGB_666666));
						mRLRight.setBackgroundResource(R.drawable.selector_gender_right);
					}
					break;
					
				default:
					event.getAction();
					break;
				}
				return false;
			}
		});
	}
	
	private void changeSelected() {
		if(mCurrentItem == DEFAULT){
			mRLLeft.setSelected(false);
			mRLRight.setSelected(false);
			mTvLeft.setTextColor(getResources().getColor(R.color.RGB_666666));
			mTvRight.setTextColor(getResources().getColor(R.color.RGB_666666));
			return;
		}
		if(mCurrentItem == LEFT){
			mRLLeft.setSelected(true);
			mRLRight.setSelected(false);
			if(mListenr != null){
				mListenr.leftSelected();
			}
		}else{
			mRLLeft.setSelected(false);
			mRLRight.setSelected(true);
			if(mListenr != null){
				mListenr.rightSelected();
			}
		}
		mTvLeft.setTextColor(getResources().getColor(mCurrentItem == LEFT?R.color.white:R.color.RGB_666666));
		mTvRight.setTextColor(getResources().getColor(mCurrentItem == RIGHT?R.color.white:R.color.RGB_666666));
	}
	
	public static interface SelectButtonListener{
		public void leftSelected();
		public void rightSelected();
	}
	
	public void setLeftText(String str){
		mTvLeft.setText(str);
	}
	
	public void setRightText(String str){
		mTvRight.setText(str);
	}
	
}
